package com.aplink.generic.widgets;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

import com.aplink.generic.GenericFragment;
import com.aplink.generic.constant.type.TabhostDisplay;
import com.aplink.generic.dictionary.GenericDictionary;
import com.aplink.generic.interfaces.OnGetObjProInfo;
import com.aplink.generic.pojo.GenericAttribute;
import com.aplink.generic.pojo.PageItem;
import com.aplink.generic.pojo.controlspec.ControlSpec;
import com.aplink.generic.pojo.controlspec.tabhost.TabHostSpec;
import com.aplink.generic.util.Language;
import com.aplink.sg.R;

public class ApLinkTabhost extends ApLinkBaseView {
	private final FragmentTabHost mTabHost;
	private TabHostSpec mTabhostFormat = null;

	public ApLinkTabhost(final GenericFragment genericFragment,
	        final ControlSpec controlSpec, final View view, Bundle bundle) {
		super(genericFragment, controlSpec, view, bundle);
		this.mTabHost = (FragmentTabHost) view;
		this.mTabHost.setup(getApplicationContext(), getChildFragmentManager(),
		        R.id.realtabcontent);
		this.mTabhostFormat = (TabHostSpec) controlSpec;
	}

	@Override
	public void clear() {

	}

	@Override
	public GenericAttribute getAttribute() {
		return null;
	}

	@Override
	public GenericDictionary<String, GenericAttribute> getListAttributes() {
		return null;
	}

	@Override
	public FragmentTabHost getView() {
		return this.mTabHost;
	}

	@Override
	protected void initViewProperties() {
		if (this.mTabhostFormat.getBackgroundColor() != null) {
			this.mTabHost.setBackgroundResource(this.mTabhostFormat
			        .getBackgroundColorId());
		}
		for (int i = 0; i < this.mTabhostFormat.getPageCollection().size(); i++) {
			final PageItem tabItem = this.mTabhostFormat.getPageCollection()
			        .get(i);
			final String title = tabItem.getTitle();
			final TabSpec tabSpec = this.mTabHost.newTabSpec(tabItem.getName());
			if (this.mTabhostFormat.getDisplay().equals(TabhostDisplay.Title)) {
				tabSpec.setIndicator(title, null);
			} else if (this.mTabhostFormat.getDisplay().equals(
			        TabhostDisplay.Icon)) {
				final Drawable drawable = getResources().getDrawable(
				        tabItem.getIconID());
				tabSpec.setIndicator("", drawable);
			} else if (this.mTabhostFormat.getDisplay().equals(
			        TabhostDisplay.TitleAndIcon)) {
				final LinearLayout view = (LinearLayout) LayoutInflater.from(
				        getApplicationContext())
				        .inflate(R.layout.aplink__generic_tabhost_indicator,
				                null, false);
				final TextView textView = (TextView) view
				        .findViewById(android.R.id.title);
				final ImageView imageView = (ImageView) view
				        .findViewById(android.R.id.icon);
				final Drawable drawable = getResources().getDrawable(
				        tabItem.getIconID());
				imageView.setImageDrawable(drawable);
				Language.getInstance().setText(textView, title);
				view.setTag(textView);
				tabSpec.setIndicator(view);
			}
			final Bundle bundle = new Bundle();
			bundle.putInt(GenericFragment.FORM_DESIGN,
			        tabItem.getFormDesignID());
			bundle.putInt(GenericFragment.CONTENT_DATA,
			        tabItem.getContentDataID());
			this.mTabHost.addTab(tabSpec, GenericFragment.class, bundle);
			final View anchorView = this.mTabHost.getTabWidget().getChildAt(i);
			if (tabItem.getPopupMenuSpec() != null) {
				final ApLinkPopupMenu customPopupMenu = new ApLinkPopupMenu(
				        getGenericFragment(), tabItem.getPopupMenuSpec(),
				        anchorView, null);
				customPopupMenu.initView();
			}
		}
		this.mTabHost.setCurrentTab(this.mTabhostFormat.getStartTab());
	}

	@Override
	public void languageChanged() {
		for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) {
			View view = mTabHost.getTabWidget().getChildAt(i);
			TextView textView = (TextView) view.getTag();
			Language.getInstance().setText(textView, null);
		}
	}

	@Override
	protected void loadBindingObject() {

	}

	@Override
	protected void loadDefaultObject() {

	}

	@Override
	protected void setEventListener() {

	}

	@Override
	public void setObjPropInfoTemp(final OnGetObjProInfo getObjProInfo) {
		throw new UnsupportedOperationException(this.getClass().getSimpleName()
		        + " is not support this method");
	}

	@Override
	public void setOnClickListener(final OnClickListener onClickListener) {
		throw new UnsupportedOperationException(this.getClass().getSimpleName()
		        + " is not support this method");
	}
}
